'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en'], function ($) {
  return {
    
    init: function () {
      this.initPage();
      this.initResource();
      this.beforeSubmit();
    },

    initPage : function() {
      $(document).find('form').bt_validate();
    },
    
    initResource : function() {
      
       $(document).on('click', '.role-resource .panel-heading input[type="checkbox"]', function (e) {
         
         var id = $(e.currentTarget).attr("id");
         
         // the meun_table_id corresponding to current select menu 
         var menuId = id + "-table";
         
         // the submenu'class belonging to the table
         var $submenu = $('#'+ menuId + ' .submenu-hidden');
         
         // the leafmenu'class belonging to the table
         var $leafmenu = $('#'+ menuId + ' input[type="checkbox"]');
         
         if($(e.currentTarget).is(':checked')){  //choose the menu
           
           $submenu.prop("checked", true);
           $leafmenu.prop("checked", true);
           
         } else {
           $submenu.prop("checked", false);
           $leafmenu.prop("checked", false);
         }
         
       });
       
    },
    
    beforeSubmit: function() {
      $("form").submit(function(e){
          
        
          //1. validate input 
          $(document).find('form').bt_validate();
        
          
          //2. validate role name, role name cannot repeate
          var name = $('input[name="name"]').val().trim();
          
          if(name !=null && name!= undefined && name!=""){
        	  
        	  $.ajax({
              url:window.__CTX__.root + "/roles?exsitName=true&roleName="+name,
              type:"GET",
            }).done(function(data){
              
              data = typeof data === 'String' ? JSON.parse(data) : data
              
              if(data.data.existName) {
                $('input[name="name"]').show_err_tooltip('该角色名称已存在！');
                return false;
              }
              
            }).fail(function(){
          	  return false;
            })
          }
          
          
          
          //3. validate checkbox, at lest select one
          var isChooseOne = false;
          var checkbox = $('input[name="menus"]');
          for(var i = 0; i < checkbox.length; i++){
            if(checkbox[i].checked){
          	  isChooseOne = true;
          	  break;
            }
              
          }
          if(!isChooseOne){
            $(".resource-panel").show_err_tooltip('菜单权限不能为空！');
            return false;
          }
          
          
          //4. check if submenu need to be selected, according to its leafmenu
          $('.submenu-hidden input[type="checkbox"]').each(function(i,e){
            
            var submenuInputClass = $(e).attr("class");
            var isChooseSubmenu = false;
            
            // if exist one checked leaf of this submenu, set the submenu checked
            $('.' + submenuInputClass + '-leafmenu').each(function(i,e){
              
              if($(e).is(":checked")){
                isChooseSubmenu = true;
                return false
              }
            })
            
            if(isChooseSubmenu)
              $(e).prop("checked",true);
            else
              $(e).prop("checked",false);
             
          })
          
          
          //5. check if the unchecked menu need to be selected, according to its submenu
          $('.role-resource .panel-heading input[type="checkbox"]').each(function(i,e){
            
            //it the menu is unchecked
            if(!$(e).is(":checked")){
              
              var tableId = $(e).attr("id") + "-table";
              var isChooseMenu = false;
              
              // if exist one checked submenu of this menu, set the menu checked
              $('#'+ tableId +' .submenu-hidden input[type="checkbox"]').each(function(i,e){
                  
                if($(e).is(":checked")){
                  isChooseMenu = true;
                  return false
                }
              })
                
              if(isChooseMenu) {
                $(e).prop("checked",true);
              } else {
                $(e).prop("checked",false);
              }
              
            }
               
          })
             
        });
     }
     
  }
})

